package com.sanjiahantang.youjiarole.module.common.presenters;

import android.content.Context;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiarole.module.common.beans.CustomerBean;
import com.sanjiahantang.youjiarole.module.common.beans.DataBean;
import com.sanjiahantang.youjiarole.module.common.beans.PersonGoodsBean;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.common.beans.UserBean;
import com.sanjiahantang.youjiarole.module.common.beans.UserOBean;
import com.sanjiahantang.youjiarole.module.common.models.IUserModel;
import com.sanjiahantang.youjiarole.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiarole.module.common.views.MeAccountView;
import com.sanjiahantang.youjiarole.module.money.beans.BankCardBean;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/10/28.
 */

public class MeAccountPresenter extends BaseMvpPresenter<MeAccountView> {

    IUserModel userModel;
    BaseView.OnLoadListener onLoadListener;

    public MeAccountPresenter() {
        userModel = new UserModel();
    }

    //提现
    public void WithdrawalsMoney(Context context, String money, String pwd, String bankno, final BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put("userLogin", "" + SPUtils.get(context, SPConfig.PHONE, ""));
        map.put("userName", "" + SPUtils.get(context, SPConfig.USERNAME, ""));
        map.put("money", money);
        map.put("mode", "5");
        map.put("state", "1");
        map.put("accout", bankno);
        userModel.WithdrawalsMoney(context, map, new CallBack<UserBean> (){

            @Override
            public void onSuccess(UserBean response, Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response);
                } else if(response != null && !TextUtils.isEmpty(response.getMessage())){
                    Utils.showToast(context,response.getMessage());
                } else {
                    listener.onLoad(null);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                listener.onLoad(null);
            }
        });

    }

    public void setOncliclkLisener(BaseView.OnLoadListener oncliclkLisener) {
        this.onLoadListener = oncliclkLisener;
    }

    //提现明细
    public void WithdrawalsMoneyDetail(Context context, int type, String orderCode, final BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PAGE, 1);
        param.put(KEY_PAGE_SIZE, DEFAULT_PAGE_SIZE);
        Map<String, Object> map = new HashMap<>();
        param.put(KEY_PARAM, map);
        map.put("userLogin", SPUtils.get(context, SPConfig.PHONE, ""));

        switch (type) {
            case 0:
                break;
            case 1:
                map.put("action", 1);
                map.put("userRole", 1);
                break;
            case 2:
                map.put("action", 1);
                map.put("userRole", 4);
                break;
            case 3:
                map.put("action", 1);
                map.put("userRole", 2);
                break;
            case 4:
                map.put("action", 1);
                map.put("userRole", 3);
                break;
            case 5:
                map.put("action", 2);
                break;
            default:
                break;
        }
        if (!TextUtils.isEmpty(orderCode)) {
            map.put("orderCode", orderCode);
        }
        userModel.LoadMoneyDetail(context, param, new CallBack<UserOBean>() {

            @Override
            public void onSuccess(UserOBean response, Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    Object[] objects = new Object[2];
                    objects[0] = ACCOUNT_TYPE_COMMEN;
                    objects[1] = response.getList();
                    listener.onLoad(objects);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    //我的物料管理
    public void MyMeteriaManager(Context context) {
        mView.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put(KEY_PHONE, "" + SPUtils.get(context, SPConfig.PHONE, ""));
        userModel.MyMeteriaManager(context, map, new MyMeteriaManagerCallBack());
    }

    //载入客户数据
    public void onLoadCustomer(Context context, String type, String keyword) {
        mView.showProgress();
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put(KEY_PAGE, 1);
        map.put(KEY_PAGE_SIZE, DEFAULT_PAGE_SIZE);
        Map<String, Object> params = new HashMap<>();
        params.put(KEY_ROLE_ID, type);
        params.put(KEY_KEYWORDS, keyword);
        params.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        map.put(KEY_PARAM, params);
        userModel.LoadCustomerList(context, map, new OnLoadCustomeListCallBack());
    }

    //添加我的项目合伙人
    public void onLoadMyPartner(Context context, String keyword, String roleType) {
        mView.showProgress();
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));

        int stateId = 0;
        if (TextUtils.isEmpty(roleType)) {
            stateId = 1;
        } else if ("0".equals(roleType) || "1".equals(roleType) || "2".equals(roleType)) {
            stateId = 1;
        } else if ("3".equals(roleType)) {
            stateId = 0;
            roleType = "0";
        } else if ("4".equals(roleType)) {
            stateId = 0;
            roleType = "1";
        }

        map.put(KEY_ROLE_ID, roleType);
        map.put("stateId", stateId);
        map.put("fuzzyMatching", keyword);
        userModel.LoadCustomerMypartner(context, map, new OnLoadCustomeMypartnerCallBack());
    }

    //查看银行卡信息
    public void CheckBankInfo(Context context, final BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        userModel.CheckBankInfo(context, map, new CallBack<BankCardBean.Card>() {

            @Override
            public void onSuccess(BankCardBean.Card response, Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response);
                }
                Utils.showToast(context, response.getMessage());
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    //查看银行卡列表
    public void CheckBankListInfo(Context context, final BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        map.put("isCustomer","0");
        userModel.CheckBankListInfo(context, map, new CallBack<BankCardBean.CardList>() {

            @Override
            public void onSuccess(BankCardBean.CardList response, Context context, Object tag) {
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    if (response.getData().size() > 0) {
                        ConfigValue.banklist = response.getData();
                        Object[] objects = new Object[2];
                        objects[0] = ACCOUNT_TYPE_COMMEN;
                        objects[1] = response.getData();
                        listener.onLoad(objects);
                    }
                }
                Utils.showToast(context, response.getMessage());
            }

            @Override
            public void onFailure(String message) {
                if (mView != null) mView.hideProgress();
            }
        });
    }

    //添加银行卡
    public void addBank(Context context, String name, String carNO, String bankName, String phone, String code) {

        /**
         * {
         "bankPhone": "string",
         "cardName": "string",
         "cardNo": "string",
         "mbciId": 0,
         "openingBank": "string",
         "phone": "string",
         "token": "string",
         "userName": "string"
         }
         */
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put("cardNo", carNO);
        map.put("bankPhone", phone);
        map.put("cardName", name);
        map.put("userName", "张三");
        map.put("openingBank", bankName);
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        // map.put("phone", "15510563729");
        mView.showProgress();
        userModel.AddbankInfo(context, map, new AddBankInfoCallBack());
    }

    public void findMoneyRecordNumberInfo(Context context, int type, String orderCode, final BaseView.OnLoadListener listener) {
        mView.showProgress();
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("userLogin", SPUtils.get(context, SPConfig.PHONE, ""));

        switch (type) {
            case 0:
                break;
            case 1:
                param.put("action", 1);
                param.put("userRole", 1);
                break;
            case 2:
                param.put("action", 1);
                param.put("userRole", 4);
                break;
            case 3:
                param.put("action", 1);
                param.put("userRole", 2);
                break;
            case 4:
                param.put("action", 1);
                param.put("userRole", 3);
                break;
            case 5:
                param.put("action", 2);
                break;
            default:
                break;
        }
        if (!TextUtils.isEmpty(orderCode)) {
            param.put("orderCode", orderCode);
        }

        userModel.findMoneyRecordNumberInfo(context, param, new CallBack<DataBean>() {
            @Override
            public void onSuccess(DataBean response, Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    public void updateDefaultBankCard(Context context, String mbciId, final BaseView.OnLoadListener listener) {
        Map<String,String> map = new HashMap<>();
        map.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        map.put("mbciId",mbciId);
        map.put(KEY_PHONE, (String) SPUtils.get(context,SPConfig.PHONE,""));
        userModel.updateDefaultBankCard(context,map, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                listener.onLoad(response);
            }

            @Override
            public void onFailure(String message) {
                listener.onLoad(message);
            }
        });
    }

    public void addBankCardInfo(Context context,
                                ConditionBean.SelectorData bankData,
                                String bankNo,
                                String holder,
                                String idCard,
                                String phone,
                                ConditionBean.SelectorData typeData,
                                String openBank,
                                String strProvince,
                                String strCity,
                                final BaseView.OnLoadListener listener) {
        boolean isValid = true;
        String message = "";
        if (bankData == null){
            isValid = false;
            message = "请选择银行名称";
        }

        if (isValid && TextUtils.isEmpty(bankNo)){
            isValid = false;
            message = "请输入银行卡号";
        }

        if (isValid && TextUtils.isEmpty(holder)){
            isValid = false;
            message = "请输入持卡人姓名";
        }

        if (isValid && TextUtils.isEmpty(idCard)){
            isValid = false;
            message = "请输入身份证号";
        }

        if (isValid && TextUtils.isEmpty(phone)){
            isValid = false;
            message = "请输入手机号";
        }

        if (isValid && typeData == null){
            isValid = false;
            message = "请选择对公或个人";
        }

        if (isValid && TextUtils.isEmpty(openBank)){
            isValid = false;
            message = "请输入开户行名称";
        }

        if (isValid && TextUtils.isEmpty(strProvince)){
            isValid = false;
            message = "请选择开户行地址";
        }

        if (isValid){
            Map<String, Object> map = new HashMap<>();
            map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
            map.put("bankCardName", holder);
            map.put("bankCardNo", bankNo);
            map.put("bankCardType", "01");
            map.put("bankCode", bankData.getDicValue());
            map.put("bankGenneralName", bankData.getDicName());
            map.put("bankPhone", phone);
            map.put("city", strCity);
            map.put("idCard", idCard);
            map.put("isCustomer", "1");
            map.put("isDefault", "0");
            map.put("openingBankAddr", strProvince + " " + strCity);
            map.put("openingBankName", openBank);
            map.put("provcince", strProvince);
            map.put("toPublicOrPrivate", typeData.getDicValue());
            if (mView == null) return;
            mView.showProgress();
            userModel.AddbankInfo(context, map, new CallBack<UserOBean>() {
                @Override
                public void onSuccess(UserOBean response, Context context, Object tag) {
                    listener.onLoad(response.getCode() == RESPONSE_CODE_SUC ? response : null);
                }

                @Override
                public void onFailure(String message) {
                    listener.onLoad(null);
                }
            });
        }else{
            Utils.showToast(context,message);
        }
    }

    public class MyMeteriaManagerCallBack implements CallBack<PersonGoodsBean> {

        @Override
        public void onSuccess(PersonGoodsBean response, Context context, Object tag) {
            if (mView == null) return;
            hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC) {

                Object[] objects = new Object[2];
                objects[0] = ACCOUNT_TYPE_COMMEN;
                objects[1] = response.getData();
                onLoadListener.onLoad(objects);
            }
        }

        @Override
        public void onFailure(String message) {
            if (mView == null) return;
            hideProgress();
        }
    }

    private void hideProgress() {
        if (mView != null) mView.hideProgress();
    }


    public class AddBankInfoCallBack implements CallBack<UserOBean> {

        @Override
        public void onSuccess(UserOBean response, Context context, Object tag) {
            hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC) {

            }
            Utils.showToast(context, response.getMessage());
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }


    public class OnLoadCustomeListCallBack implements CallBack<CustomerBean> {

        @Override
        public void onSuccess(CustomerBean response, Context context, Object tag) {
            hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC) {
                Object[] objects = new Object[2];
                objects[0] = ACCOUNT_TYPE_COMMEN;
                objects[1] = response.getData().getList();
                onLoadListener.onLoad(objects);
            }
            Utils.showToast(context, response.getMessage());
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }

    public class OnLoadCustomeMypartnerCallBack implements CallBack<UserOBean> {
        @Override
        public void onSuccess(UserOBean response, Context context, Object tag) {
            hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC) {
                Object[] objects = new Object[2];
                objects[0] = ACCOUNT_TYPE_COMMEN;
                objects[1] = response.getData();
                onLoadListener.onLoad(objects);
            }
            Utils.showToast(context, response.getMessage());
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }
}
